package com.navinfo.platform.trip.common.arithmetic;

import com.navinfo.platform.trip.common.arithmetic.data.CommonData;
import com.navinfo.platform.trip.common.arithmetic.data.OuterStatisticData;
import com.navinfo.platform.trip.common.arithmetic.var.VarCommon;
import com.navinfo.platform.trip.common.arithmetic.var.VarRangeTorque;
import com.navinfo.platform.trip.common.arithmetic.enums.RangeTorqueEnum;
import com.navinfo.platform.trip.common.arithmetic.manager.AbstractArithmetic;
import com.navinfo.platform.trip.common.util.ArithmeticUtils;

import java.util.Optional;

/**
 * 发动机扭矩区间算法<br>
 * 依赖{@link IdleParking}的怠速状态.<br>
 * 输出：统计
 * @author 沈东生
 */
public class RangeTorque extends AbstractArithmetic<VarRangeTorque> {
    @Override
    public void calculate() {
        logger.debug("进入发动机扭矩区间算法.");
        CommonData commonData = getCommonData();
        int engineOutputTorque = commonData.getEngineOutputTorque();

        if (!RangeTorqueEnum.RANGE_VALID.getRange().contains(engineOutputTorque)) {
            return;
        }

        VarCommon varTorque = null;
        VarRangeTorque var = getObj();
        if ( commonData.isWork()) {
            if (RangeTorqueEnum.RANGE_0.getRange().contains(engineOutputTorque)) {
                varTorque = var.getTorquePercent0();
            } else if (RangeTorqueEnum.RANGE_10.getRange().contains(engineOutputTorque)) {
                varTorque = var.getTorquePercent10();
            } else if (RangeTorqueEnum.RANGE_20.getRange().contains(engineOutputTorque)) {
                varTorque = var.getTorquePercent20();
            } else if (RangeTorqueEnum.RANGE_30.getRange().contains(engineOutputTorque)) {
                varTorque = var.getTorquePercent30();
            } else if (RangeTorqueEnum.RANGE_40.getRange().contains(engineOutputTorque)) {
                varTorque = var.getTorquePercent40();
            } else if (RangeTorqueEnum.RANGE_50.getRange().contains(engineOutputTorque)) {
                varTorque = var.getTorquePercent50();
            } else if (RangeTorqueEnum.RANGE_60.getRange().contains(engineOutputTorque)) {
                varTorque = var.getTorquePercent60();
            } else if (RangeTorqueEnum.RANGE_70.getRange().contains(engineOutputTorque)) {
                varTorque = var.getTorquePercent70();
            } else if (RangeTorqueEnum.RANGE_80.getRange().contains(engineOutputTorque)) {
                varTorque = var.getTorquePercent80();
            } else if (RangeTorqueEnum.RANGE_90.getRange().contains(engineOutputTorque)) {
                varTorque = var.getTorquePercent90();
            } else if (RangeTorqueEnum.RANGE_99.getRange().contains(engineOutputTorque)) {
                varTorque = var.getTorquePercent99();
            } else if (RangeTorqueEnum.RANGE_100.getRange().contains(engineOutputTorque)) {
                varTorque = var.getTorquePercent100();
            }

            //累加相应转速区间的公共统计变量
            if(null != varTorque){
                varTorque.addMileage(commonData.getDffMileage());
                varTorque.addFuel(commonData.getDffFuel());
                varTorque.addDuration(commonData.getDffTime());
            }
        }

        if (var.getMaxTorque() == null || engineOutputTorque > var.getMaxTorque()) {
            var.setMaxTorque(engineOutputTorque);
        }
        if (var.getMinTorque() == null || engineOutputTorque < var.getMinTorque()) {
            var.setMinTorque(engineOutputTorque);
        }

        var.setTorqueSum(Optional.ofNullable(var.getTorqueSum()).orElse(0L) + engineOutputTorque);
        var.setTorqueCount(Optional.ofNullable(var.getTorqueCount()).orElse(0) + 1);
        logger.debug("离开发动机扭矩区间算法.");
    }

    @Override
    public void collect(OuterStatisticData statisticDataOut) {
        VarRangeTorque var = getObj();
        statisticDataOut.setTorque0Duration(ArithmeticUtils.timeMsToSecond(var.getTorquePercent0().getDuration()));
        statisticDataOut.setTorque0Mileage(ArithmeticUtils.mileageMToM(var.getTorquePercent0().getMileage()));
        statisticDataOut.setTorque0Fuel(ArithmeticUtils.fuelMlToMl(var.getTorquePercent0().getFuel()));

        statisticDataOut.setTorque10Duration(ArithmeticUtils.timeMsToSecond(var.getTorquePercent10().getDuration()));
        statisticDataOut.setTorque10Mileage(ArithmeticUtils.mileageMToM(var.getTorquePercent10().getMileage()));
        statisticDataOut.setTorque10Fuel(ArithmeticUtils.fuelMlToMl(var.getTorquePercent10().getFuel()));

        statisticDataOut.setTorque20Duration(ArithmeticUtils.timeMsToSecond(var.getTorquePercent20().getDuration()));
        statisticDataOut.setTorque20Mileage(ArithmeticUtils.mileageMToM(var.getTorquePercent20().getMileage()));
        statisticDataOut.setTorque20Fuel(ArithmeticUtils.fuelMlToMl(var.getTorquePercent20().getFuel()));

        statisticDataOut.setTorque30Duration(ArithmeticUtils.timeMsToSecond(var.getTorquePercent30().getDuration()));
        statisticDataOut.setTorque30Mileage(ArithmeticUtils.mileageMToM(var.getTorquePercent30().getMileage()));
        statisticDataOut.setTorque30Fuel(ArithmeticUtils.fuelMlToMl(var.getTorquePercent30().getFuel()));

        statisticDataOut.setTorque40Duration(ArithmeticUtils.timeMsToSecond(var.getTorquePercent40().getDuration()));
        statisticDataOut.setTorque40Mileage(ArithmeticUtils.mileageMToM(var.getTorquePercent40().getMileage()));
        statisticDataOut.setTorque40Fuel(ArithmeticUtils.fuelMlToMl(var.getTorquePercent40().getFuel()));

        statisticDataOut.setTorque50Duration(ArithmeticUtils.timeMsToSecond(var.getTorquePercent50().getDuration()));
        statisticDataOut.setTorque50Mileage(ArithmeticUtils.mileageMToM(var.getTorquePercent50().getMileage()));
        statisticDataOut.setTorque50Fuel(ArithmeticUtils.fuelMlToMl(var.getTorquePercent50().getFuel()));

        statisticDataOut.setTorque60Duration(ArithmeticUtils.timeMsToSecond(var.getTorquePercent60().getDuration()));
        statisticDataOut.setTorque60Mileage(ArithmeticUtils.mileageMToM(var.getTorquePercent60().getMileage()));
        statisticDataOut.setTorque60Fuel(ArithmeticUtils.fuelMlToMl(var.getTorquePercent60().getFuel()));

        statisticDataOut.setTorque70Duration(ArithmeticUtils.timeMsToSecond(var.getTorquePercent70().getDuration()));
        statisticDataOut.setTorque70Mileage(ArithmeticUtils.mileageMToM(var.getTorquePercent70().getMileage()));
        statisticDataOut.setTorque70Fuel(ArithmeticUtils.fuelMlToMl(var.getTorquePercent70().getFuel()));

        statisticDataOut.setTorque80Duration(ArithmeticUtils.timeMsToSecond(var.getTorquePercent80().getDuration()));
        statisticDataOut.setTorque80Mileage(ArithmeticUtils.mileageMToM(var.getTorquePercent80().getMileage()));
        statisticDataOut.setTorque80Fuel(ArithmeticUtils.fuelMlToMl(var.getTorquePercent80().getFuel()));

        statisticDataOut.setTorque90Duration(ArithmeticUtils.timeMsToSecond(var.getTorquePercent90().getDuration()));
        statisticDataOut.setTorque90Mileage(ArithmeticUtils.mileageMToM(var.getTorquePercent90().getMileage()));
        statisticDataOut.setTorque90Fuel(ArithmeticUtils.fuelMlToMl(var.getTorquePercent90().getFuel()));

        statisticDataOut.setTorque99Duration(ArithmeticUtils.timeMsToSecond(var.getTorquePercent99().getDuration()));
        statisticDataOut.setTorque99Mileage(ArithmeticUtils.mileageMToM(var.getTorquePercent99().getMileage()));
        statisticDataOut.setTorque99Fuel(ArithmeticUtils.fuelMlToMl(var.getTorquePercent99().getFuel()));

        statisticDataOut.setTorque100Duration(ArithmeticUtils.timeMsToSecond(var.getTorquePercent100().getDuration()));
        statisticDataOut.setTorque100Mileage(ArithmeticUtils.mileageMToM(var.getTorquePercent100().getMileage()));
        statisticDataOut.setTorque100Fuel(ArithmeticUtils.fuelMlToMl(var.getTorquePercent100().getFuel()));

        statisticDataOut.setTorqueSum(var.getTorqueSum());
        statisticDataOut.setTorqueCount(var.getTorqueCount());

        statisticDataOut.setMaxTorque(var.getMaxTorque());
        statisticDataOut.setMinTorque(var.getMinTorque());

    }
}
